//
//  AddAddressViewController.m
//  体育商城
//
//  Created by  on 2022/9/24.
//

#import "AddAddressViewController.h"

@interface AddAddressViewController ()<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet BaseTextField *input1;
@property (strong, nonatomic) IBOutlet BaseTextField *input2;
@property (strong, nonatomic) IBOutlet BaseTextField *input3;
@property (strong, nonatomic) IBOutlet UISwitch *switchBtn;
@end

@implementation AddAddressViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}
- (void)initUI{
    self.title = LS(@"编辑收货地址");
    if (self.model == nil) {
        self.model = [Address new];
        self.model.name = @"";
        self.model.phone = @"";
        self.model.address = @"";
        self.model.account = APP.user.account;
        self.model.selected = NO;
        self.model.isdefault = NO;
        self.model.ID = [NSString stringWithFormat:@"%d",self.account + 1];
    }
    self.input1.text = self.model.name;
    self.input2.text = self.model.phone;
    self.input3.text = self.model.address;
    self.switchBtn.on = self.model.isdefault;
}
- (IBAction)actionForSwitch:(UISwitch *)sender {
    self.model.isdefault = sender.on;
    if (self.model.isdefault) {
        APP.user.address = self.model;
    }
}
- (IBAction)actionForBtn:(UIButton *)sender {
    if (self.input1.text.length <= 0) {
        return [Toast msg:LS(@"收货人名字不能为空")];
    }else if (self.input2.text.length <= 0){
        return [Toast msg:LS(@"收货人联系电话不能为空")];
    }else if (self.input3.text.length <= 0){
        return [Toast msg:LS(@"收货地址不能为空")];
    }
    self.model.name = self.input1.text;
    self.model.phone = self.input2.text;
    self.model.address = self.input3.text;
    [Toast startHud:LS(@"保存中...")];
    [AddressTool update:@[self.model] complete:^{
        if (self.account <= 0) {
            [[NSNotificationCenter defaultCenter] postNotificationName:notifyAddressUpdate object:nil];
            [Toast startHud:LS(@"保存成功") stopTime:1 handle:nil];
        }else{
            [AddressTool find:APP.user.account complete:^(NSArray * _Nonnull result) {
                
                for (Address *m in result) {
                    m.isdefault = [m.ID isEqualToString:self.model.ID] && self.model.isdefault;
                }
                
                [AddressTool update:result complete:^{
                    [[NSNotificationCenter defaultCenter] postNotificationName:notifyAddressUpdate object:nil];
                    [Toast startHud:LS(@"保存成功") stopTime:1 handle:nil];
                }];
            }];
        }
    }];
    
}
#pragma mark - UITableViewDataSource
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    textField.keyboardType = textField == self.input2 ? UIKeyboardTypeNumberPad : UIKeyboardTypeDefault;
    return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField == self.input2 && [string isEqualToString:@""] == NO){
        return [string isNumberOrAlphabet];
    }
    return YES;
}
@end
